﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;
using Core.Utils;
using System.Security.Permissions;

namespace ScoutNET.ActionsResults
{
    public class UploadPicturesResult : ActionResult
    {
        #region Properties
        public HttpPostedFileBase FileData { get; set; }
        public ImageMode ImageMode{ get; set; }
        public string Folder { get; set; }
        public string ImageName { get; set; }
        public byte Quality { get; set; }
        public int ImageID { get; set; }
        #endregion

        #region Constructor
        public UploadPicturesResult(HttpPostedFileBase fileData, string folder, string imageName, ImageMode imageMode)
        {
            this.Folder = folder;
            this.FileData = fileData;
            this.ImageName = imageName;
            this.ImageMode = imageMode;
            this.Quality = 100;
        }

        public UploadPicturesResult(HttpPostedFileBase fileData, string folder, string imageName, ImageMode imageMode, int imageID)
        {
            this.Folder = folder;
            this.FileData = fileData;
            this.ImageName = imageName;
            this.ImageMode = imageMode;
            this.ImageID = imageID;
            this.Quality = 100;
        }
        #endregion

        #region Methodes
        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public override void ExecuteResult(ControllerContext context)
        {
            UploadImage(context);
        }

        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        private void UploadImage(ControllerContext context)
        {
            try
            {
                Dictionary<string, int> imageSize = Imaging.GetImageSize(this.ImageMode);

                BinaryReader b = new BinaryReader(this.FileData.InputStream);
                byte[] baImage = b.ReadBytes(this.FileData.ContentLength);

                baImage = Imaging.ResizeImage(baImage, imageSize["W"], imageSize["H"], this.Quality);

                string urlpath = string.Empty;

                if (this.Folder != null)
                {
                    urlpath = context.HttpContext.Server.UrlDecode(this.Folder);
                    urlpath = context.HttpContext.Server.MapPath(urlpath);

                    if (!Directory.Exists(urlpath))
                    {
                        Directory.CreateDirectory(urlpath);
                    }

                    Files.ByteArrayToFile(urlpath + this.ImageName, baImage);
                }

                int height = -1;
                int width = -1;
                Imaging.GetImageSize(baImage, out width, out height);

                string imageID = this.ImageID.ToString();

                context.HttpContext.Response.ContentType = "application/json";
                context.HttpContext.Response.Expires = -1;
                context.HttpContext.Response.Write(@"{""w"":" + width + @", ""h"":" + height + @", ""ID"" : """ + imageID + @"""}");
                context.HttpContext.Response.StatusCode = 200;
            }
            catch (Exception ex)
            {
                context.HttpContext.Response.ContentType = "application/json";
                string error = @"{error: """ + ex.Message + @"""}";
                context.HttpContext.Response.StatusCode = 500;
                context.HttpContext.Response.Write(error);
            }
            
            context.HttpContext.Response.End();
        }
        #endregion
    }
}
